package adminapilogic

import (
	v1 "code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/admin/v1"
	"code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/common"
	"code.bydev.io/cht/customer/kyc-stub.git/pkg/bybit/kyc/common/enums"
	"code.bydev.io/cht/customer/kyc_admin_service/internal/app/serv"
	"context"
	pb "git.bybit.com/svc/stub/pkg/pb/api/kycadmin"
	"git.bybit.com/svc/stub/pkg/pb/enums/ekycadmin"
	"git.bybit.com/svc/stub/pkg/pb/enums/kyc"

	"code.bydev.io/cht/customer/kyc_admin_service/service/private/internal/svc"
)

type AdminAPIGetKycActionListLogic struct {
	svcCtx *svc.ServiceContext
}

func NewAdminAPIGetKycActionListLogic(
	svcCtx *svc.ServiceContext,
) *AdminAPIGetKycActionListLogic {
	return &AdminAPIGetKycActionListLogic{
		svcCtx: svcCtx,
	}
}

// KYC 认证查询列表
func (l *AdminAPIGetKycActionListLogic) GetKycActionList(
	ctx context.Context,
	in *v1.GetKycActionListRequest,
) (*v1.GetKycActionListResponse, error) {
	oldResp, err := serv.NewService(l.svcCtx.NothingEnv).GetKycActionList(ctx, &pb.GetKycActionListRequest{
		MemberId:   in.GetMemberId(),
		RecordId:   in.GetRecordId(),
		Provider:   kyc.KYCProvider(in.GetProvider()),
		Decision:   ekycadmin.KYCAdminDecision(in.GetDecision()),
		Country:    in.GetCountry(),
		DocNumber:  in.GetDocNumber(),
		PageNo:     in.GetPageNo(),
		PageSize:   in.GetPageSize(),
		EnableFlow: in.GetEnableFlow(),
		BrokerId:   in.GetBrokerId(),
		Level:      kyc.MemberKYCLevel(in.GetLevel()),
	})
	if err != nil {
		return nil, err
	}
	var respErr *common.Error
	if oldResp.GetError() != nil {
		respErr = &common.Error{
			Code:    oldResp.GetError().GetErrorCode(),
			Message: oldResp.GetError().GetInternalDebugMessage(),
		}
	}
	var list []*v1.GetKycActionItem
	if len(oldResp.GetList()) > 0 {
		list = make([]*v1.GetKycActionItem, 0, len(oldResp.GetList()))
		for _, item := range oldResp.GetList() {
			list = append(list, &v1.GetKycActionItem{
				Id:                   item.GetId(),
				MemberId:             item.GetMemberId(),
				ReviewTime:           item.GetReviewTime(),
				Country:              item.GetCountry(),
				Provider:             item.GetProvider(),
				RecordId:             item.GetRecordId(),
				Decision:             item.GetDecision(),
				SiteId:               item.GetSiteId(),
				ProviderRejectReason: item.GetProviderRejectReason(),
				RejectReason:         item.GetRejectReason(),
				DecisionProcess:      enums.DecisionProcess(item.GetDecisionProcess()),
				Level:                enums.MemberKYCLevel(item.GetLevel()),
				IsTransfer:           item.GetIsTransfer(),
			})
		}
	}
	return &v1.GetKycActionListResponse{
		Error:    respErr,
		List:     list,
		Total:    oldResp.GetTotal(),
		PageSize: oldResp.GetPageSize(),
	}, nil
}
